package com.fesine.util;

import org.apache.log4j.Logger;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Fesine on 2016/6/14.
 */
public class FileUtil {

    private static final Logger logger = Logger.getLogger(FileUtil.class);

    public static List<String> getFileName(String path) {
        File f = new File(path);
        if (!f.exists()) {
            logger.info(path + " not exists");
            return null;
        }

        File fa[] = f.listFiles();
        Arrays.sort(fa);
        List<String> fileList = new ArrayList<>();
        for (int i = 0; i < fa.length; i++) {
            File fs = fa[i];
            if (fs.isDirectory()) {
                logger.info(fs.getName() + " [目录]");
            } else {
                logger.info(fs.getName());
                fileList.add(fs.getName());
            }
        }
        logger.info("文件数量："+fileList.size());
        return fileList;
    }

    /**
     * 以行为单位读取文件，常用于读面向行的格式化文件
     */
    public static List<String> readFileByLines(String fileName) {
        String code = null;
        try {
            code = codeString(fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        File file = new File(fileName);
        BufferedReader reader = null;
        List<String> logList = new ArrayList<>();
        try {
            logger.info("以行为单位读取文件内容，一次读一整行：");
            InputStream is = new BufferedInputStream(new FileInputStream(file));
            InputStreamReader isr = new InputStreamReader(is, code);
            reader = new BufferedReader(isr);
            //reader = new BufferedReader(new FileReader(file));
            String tempString;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                System.out.println("line " + line + ": " + tempString);
                logList.add(tempString);
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return logList;
    }

    /**
     * 判断文件的编码格式
     *
     * @param fileName :file
     * @return 文件编码格式
     * @throws Exception
     */
    public static String codeString(String fileName) throws Exception {
        BufferedInputStream bin = new BufferedInputStream(
                new FileInputStream(fileName));
        int p = (bin.read() << 8) + bin.read();
        String code = null;

        switch (p) {
            case 11552:
                code = "UTF-8";
                break;
            case 0xfffe:
                code = "Unicode";
                break;
            case 0xfeff:
                code = "UTF-16BE";
                break;
            default:
                code = "GBK";
        }
        logger.info("------>文件编码："+code);
        return code;
    }

    //public static void main(String[] args) {
    //    try {
    //        codeString("C:\\Users\\Fesine\\Desktop\\新建文本文档.txt");
    //    } catch (Exception e) {
    //        e.printStackTrace();
    //    }
    //}

}
